package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.BusinessSealtype;
import com.ruoyi.common.core.domain.entity.BusinessSys;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.service.IBusinessSealtypeService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BusinessSysMapper;
import com.ruoyi.system.service.IBusinessSysService;

/**
 * 业务系统 Service业务层处理
 *
 * @author ess
 * @date 2021-10-18
 */
@Service
public class BusinessSysServiceImpl implements IBusinessSysService {
    @Autowired
    private BusinessSysMapper businessSysMapper;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private IBusinessSealtypeService businessSealtypeService;

    /**
     * 查询业务系统
     *
     * @param bSysId 业务系统 主键
     * @return 业务系统
     */
    @Override
    public BusinessSys selectBusinessSysByBSysId(String bSysId) {
        BusinessSys businessSys = businessSysMapper.selectBusinessSysByBSysId(bSysId);
        if (businessSys!=null) {
            SysDept sysDept = deptService.selectDeptById(Long.valueOf(businessSys.getTopUnitId()));
            businessSys.setTopUnitName(sysDept.getDeptName());
        }

        return businessSys;
    }

    /**
     * 查询业务系统 列表
     *
     * @param businessSys 业务系统
     * @return 业务系统
     */
    @Override
    public List<BusinessSys> selectBusinessSysList(BusinessSys businessSys) {
//        根据单位id查找单位信息
        List<BusinessSys> businessSysList = businessSysMapper.selectBusinessSysList(businessSys);
        if (businessSysList != null && businessSysList.size() > 0) {
            for (BusinessSys sys : businessSysList) {
                SysDept sysDept = deptService.selectDeptById(Long.valueOf(sys.getTopUnitId()));
                sys.setTopUnitName(sysDept.getDeptName());
            }
        }
        return businessSysList;
    }

    /**
     * 新增业务系统
     *
     * @param businessSys 业务系统
     * @return 结果
     */
    @Override
    public int insertBusinessSys(BusinessSys businessSys) {

        BusinessSys buLast = businessSysMapper.selectOrderOne();
        if (buLast != null) {
            // 业务系统id
            String sysThree = buLast.getbSysId().replace("SYS", "");
            String numJOne = String.valueOf(Integer.valueOf(sysThree) + 1);
            String lastStr = "";
            if (numJOne.length() == 1) {
                lastStr = "00" + numJOne;
            } else if (numJOne.length() == 2) {
                lastStr = "0" + numJOne;
            } else if (numJOne.length() == 3) {
                lastStr = numJOne;
            }
            businessSys.setbSysId("SYS" + lastStr);
        } else {
            businessSys.setbSysId("SYS001");
        }

//        添加业务系统对应关系数据
        if (businessSys.getDataScope().equals("2")&&businessSys.getSealTypeIds().length>0) {
            for (int i = 0; i < businessSys.getSealTypeIds().length; i++) {
                BusinessSealtype bs = new BusinessSealtype();
                bs.setBusinessId(businessSys.getbSysId());
                bs.setSealtypeDictValue(businessSys.getSealTypeIds()[i]);
                businessSealtypeService.insertBusinessSealtype(bs);
            }
        }


        return businessSysMapper.insertBusinessSys(businessSys);
    }

    /**
     * 修改业务系统
     *
     * @param businessSys 业务系统
     * @return 结果
     */
    @Override
    public int updateBusinessSys(BusinessSys businessSys) {
        //        修改业务系统对应关系数据  先删除后添加
        if (businessSys.getDataScope().equals("2")&&businessSys.getSealTypeIds().length>0) {
            businessSealtypeService.deleteBusinessSealtypeByBusinessId(businessSys.getbSysId());

            for (int i = 0; i < businessSys.getSealTypeIds().length; i++) {
                BusinessSealtype bs = new BusinessSealtype();
                bs.setBusinessId(businessSys.getbSysId());
                bs.setSealtypeDictValue(businessSys.getSealTypeIds()[i]);
                businessSealtypeService.insertBusinessSealtype(bs);
            }
        }


        return businessSysMapper.updateBusinessSys(businessSys);
    }

    /**
     * 批量删除业务系统
     *
     * @param bSysIds 需要删除的业务系统 主键
     * @return 结果
     */
    @Override
    public int deleteBusinessSysByBSysIds(String[] bSysIds) {
//        删除对应关系表数据
      businessSealtypeService.deleteBusinessSealtypeByBusinessIds(bSysIds);

        return businessSysMapper.deleteBusinessSysByBSysIds(bSysIds);
    }

    /**
     * 删除业务系统 信息
     *
     * @param bSysId 业务系统 主键
     * @return 结果
     */
    @Override
    public int deleteBusinessSysByBSysId(String bSysId) {

        businessSealtypeService.deleteBusinessSealtypeByBusinessId(bSysId);

        return businessSysMapper.deleteBusinessSysByBSysId(bSysId);
    }
}
